﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace notepad
{
    public partial class FormMain : Form
    {
        string currentFile = string.Empty;

        public FormMain()
        {
            InitializeComponent();
        }

        public FormMain(string[] args)
        {
            InitializeComponent();
            OpenFile(args[0]);
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            this.Text = "Notepad.NET: " + Environment.UserName + "@" + Environment.MachineName;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (openFileDialogMain.ShowDialog() == DialogResult.OK)
            {
                    OpenFile(openFileDialogMain.FileName);
            }
        }

        private void OpenFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                textBoxEditor.Text =  File.ReadAllText(fileName);
                this.Text = "Notepad.NET: " + Environment.UserName + "@" + Environment.MachineName + " - " + Path.GetFileName(fileName);
                currentFile = fileName;
                SummaryTextProperties();
            }
        }

        private void CreateFile()
        {
            textBoxEditor.Text = string.Empty;
            this.Text = "Notepad.NET: " + Environment.UserName + "@" + Environment.MachineName + " - Новый";
        }

        private void FormMain_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void FormMain_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop, true);
            OpenFile(files[0]);
        }

        private void SummaryTextProperties()
        {
            string STP = string.Empty;
            STP += "Кол-во строк: " + this.textBoxEditor.Lines.Count().ToString();
            int currentLine = this.textBoxEditor.GetLineFromCharIndex(this.textBoxEditor.SelectionStart);
            int currentColumn = this.textBoxEditor.SelectionStart - textBoxEditor.GetFirstCharIndexFromLine(currentLine);

            STP += " Текущие координаты: " + (currentLine + 1).ToString() + ":" + (currentColumn + 1).ToString();

            this.toolStripStatusLabelSummaryTextProperties.Text = STP;
        }

        private void textBoxEditor_TextChanged(object sender, EventArgs e)
        {
            SummaryTextProperties();
        }

        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (currentFile.Length != 0)
            {
                DialogResult dialogResult = MessageBox.Show("", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    //do something
                }
                else if (dialogResult == DialogResult.No)
                {
                    CreateFile();
                }
                else if (dialogResult == DialogResult.Cancel)
                {
                    return;
                }

            }
        }
    }
}
